//
//  YCDropDownMenuView.m
//  YCSchool
//
//  Created by gmall on 16/5/10.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  下弹菜单

#import "YCDropDownMenuView.h"


#pragma mark -  YCDropDownMenuViewCell

@interface YCDropDownMenuViewCell : UITableViewCell

@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation YCDropDownMenuViewCell

+ (BOOL)requiresConstraintBasedLayout
{
    return YES;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.iconImageView = [[UIImageView alloc] init];
        self.iconImageView.contentMode = UIViewContentModeCenter;
        [self.contentView addSubview:self.iconImageView];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:14];
        titleLabel.textColor = [UIColor yc_colorWithUInt:0x333333 alpha:0.8];
        self.titleLabel = titleLabel;
        [self.contentView addSubview:titleLabel];
        
        [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(5);
            make.width.height.equalTo(@30);
        }];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_iconImageView.mas_right).offset(5);
            make.centerY.equalTo(self.contentView);
        }];
    }
    return self;
}

@end


#pragma mark - YCDropDownMenuView

@interface YCDropDownMenuView ()<UITableViewDataSource, UITableViewDelegate>

/** 容器  */
@property (nonatomic, strong) UIImageView * containerView;
/** 表 */
@property (nonatomic, strong) UITableView *tableView;
/** 数据  */
@property (nonatomic, strong) NSArray *dictArray;

@property (nonatomic, strong) UIButton *clearColorBtnBackView;

/** block */
@property (nonatomic, copy) didSelectRowOfIndexPathBlock selectRowBlock;

@end


@implementation YCDropDownMenuView

- (instancetype)initWithFrame:(CGRect)frame itemDictArray:(NSArray *)dictArray
{
    self = [super initWithFrame:frame];
    if (self) {
        _dictArray = dictArray;
        _containerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        UIImage *oldImage = [UIImage imageNamed:@"dropDownMenubackground_right"];
        UIImage *newImage = [oldImage stretchableImageWithLeftCapWidth:oldImage.size.width * 0.18 topCapHeight:oldImage.size.height * 0.6];
        _containerView.image = newImage;
        _containerView.alpha = 0.2;
        _containerView.contentMode = UIViewContentModeScaleToFill;
        self.clipsToBounds = YES;
        [self addSubview:_containerView];
        [self addSubview:self.tableView];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        CGFloat width = self.yc_width - 10;
        CGFloat height = self.yc_height - 15;
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(5, 10, width, height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.alwaysBounceVertical = NO;
        _tableView.showsVerticalScrollIndicator = YES;
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}


#pragma mark - TableviewDatasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dictArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"dropDownMenuViewCell";
    YCDropDownMenuViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[YCDropDownMenuViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    
    NSDictionary * rowDict = self.dictArray[indexPath.row];
    cell.iconImageView.image = [UIImage imageNamed:rowDict[@"icon"]];
    cell.titleLabel.text = rowDict[@"title"];
    return cell;
}

#pragma mark Cell点击事件处理器

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    !_selectRowBlock?:_selectRowBlock(indexPath);
    [self dismiss];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (self.yc_height - 15)/(CGFloat)self.dictArray.count;
}

#pragma mark - 显示与销毁

- (void)showWithDidSelectRowOfIndexPathBlock:(didSelectRowOfIndexPathBlock)selectRowOfIndexPathBlock
{
    _selectRowBlock = selectRowOfIndexPathBlock;
    
    CGRect frame = self.frame;
    self.clearColorBtnBackView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.clearColorBtnBackView setFrame:[UIScreen mainScreen].bounds];
    [self.clearColorBtnBackView setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:0]];
    [self.clearColorBtnBackView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self.clearColorBtnBackView];
    self.yc_height = 0;
    [self.clearColorBtnBackView addSubview:self];
    [UIView animateWithDuration:0.2f animations:^{
        self.yc_height = frame.size.height;
    }];
    
}

- (void)dismiss
{
    
    [UIView animateWithDuration:0.2f animations:^{
        self.yc_height = 0;
    } completion:^(BOOL finished) {
        [self.clearColorBtnBackView removeFromSuperview];
    }];
}


@end
